package com.example.day_blog.controller;

import com.example.day_blog.model.BlogInfo;
import com.example.day_blog.model.Comment;
import com.example.day_blog.model.Result;
import com.example.day_blog.model.UserInfo;
import com.example.day_blog.service.CommentService;
import com.example.day_blog.service.UserService;
import com.example.day_blog.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author starsea
 * @date 2024-01-27 12:43
 */
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private UserService userService;
    //获取评论区
    @RequestMapping("/getComments")
    public List<Comment> getComment(Integer blogId)
    {
        return commentService.getComments(blogId);
    }
    //添加评论
    @RequestMapping("/add")
    public Result pulishComment(Integer blogId,String content,HttpServletRequest request)
    {
        //从token中获取用户id
        System.out.println("获取到的User_token"+request.getHeader("user_token"));
        String token=request.getHeader("user_token");
        Integer id= JwtUtils.getUserIdFromToken(token);
        //通过id查找到用户信息
        UserInfo userInfo=userService.queryById(id);
        if(id<0 && id==null)
        {
            return Result.fial("用户未登录",false);
        }
        Comment comment = new Comment();
        comment.setBlogId(blogId);
        comment.setContent(content);
        comment.setUserId(id);
        comment.setUserName(userInfo.getUserName());
        comment.setUpvote(0);
        commentService.insertComment(comment);
        return Result.success(true);
    }
}
